﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace YQGL.Common
{
    public class CookiesMethod
    {
        /// <summary>   
        /// 添加cookie   
        /// </summary>   
        /// <param name="cookieName"></param>   
        /// <param name="value"></param>   
        public static void Add(string cookieName, string value, DateTime expiresTime)
        {
            //相同名字就更新
            HttpCookie cookie = new HttpCookie(HttpUtility.UrlEncode(cookieName), HttpUtility.UrlEncode(value));
            cookie.Expires = expiresTime;
            HttpContext.Current.Response.AppendCookie(cookie);
        }

        /// <summary>   
        /// 给指定的cookie的指定键添加值   
        /// </summary>   
        /// <param name="cookieName">cookie的名称</param>   
        /// <param name="keyName">要添加的键</param>   
        /// <param name="value">要添加的值</param>   
        public static void Add(string cookieName, string keyName, string value)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
            //如果cookie=null则新建一个cookie   
            if (cookie == null)
                cookie = new HttpCookie(HttpUtility.UrlEncode(cookieName));
            //如果该键不存在，则新建一个键值并赋值；否则直接赋值   
            if (string.IsNullOrEmpty(cookie.Values[keyName]))
                cookie.Values.Add(keyName, HttpUtility.UrlEncode(value));
            else
                cookie.Values[keyName] = HttpUtility.UrlEncode(value);

            HttpContext.Current.Response.AppendCookie(cookie);
        }

        /// <summary>   
        /// 读取Cookie   
        /// </summary>   
        /// <param name="key"></param>   
        /// <returns></returns>   
        public static string GetValue(string cookieName)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[HttpUtility.UrlEncode(cookieName)];
            if (cookie != null)
            {
                return string.IsNullOrEmpty(cookie.Value) ? string.Empty : HttpUtility.UrlDecode(cookie.Value);
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>   
        /// 读取指定Cookie里的指定键的值   
        /// </summary>   
        /// <param name="cookieName">cookie名称</param>   
        /// <param name="keyName">cookie的键</param>   
        /// <returns></returns>   
        public static string GetValue(string cookieName, string keyName)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[HttpUtility.UrlEncode(cookieName)];
            if (cookie == null)
                return string.Empty;
            string value = cookie.Values[HttpUtility.UrlEncode(keyName)];
            return string.IsNullOrEmpty(value) ? string.Empty : HttpUtility.UrlDecode(value);
        }

        /// <summary>   
        /// 获取指定cookie中所有的键   
        /// </summary>   
        /// <param name="cookieName"></param>   
        /// <returns></returns>   
        public static string[] GetValues(string cookieName)
        {
            if (HttpContext.Current.Request.Cookies[cookieName] == null)
                return new string[0];
            string[] keys = HttpContext.Current.Request.Cookies[cookieName].Values.AllKeys;
            for (int i = 0; i < keys.Length; i++)
            {
                keys[i] = HttpUtility.UrlDecode(keys[i]);
            }
            return keys;
        }

        /// <summary>   
        /// 移除指定的Cookie   
        /// </summary>   
        /// <param name="cookieName"></param>   
        public static void Remove(string cookieName)
        {
            HttpContext.Current.Response.Cookies.Remove(HttpUtility.UrlEncode(cookieName));
            HttpContext.Current.Response.Cookies[HttpUtility.UrlEncode(cookieName)].Expires = DateTime.Now.AddDays(-1);
        }
        /// <summary>   
        /// 移除cookie中指定的键，若是最后一个键则移除这个cookie   
        /// </summary>   
        /// <param name="cookieName"></param>   
        /// <param name="keyName"></param>   
        public static void Remove(string cookieName, string keyName)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
            if (cookie != null)
            {
                if (cookie.Values.Count > 0)
                {
                    if (cookie.Values.Count == 1)
                    {
                        //若是最后一个键则移除这个cookie,否则会多出一个空值   
                        cookie.Values.Remove(HttpUtility.UrlEncode(keyName));
                        cookie.Expires = DateTime.Now.AddDays(-1);
                    }
                    else
                    {
                        cookie.Values.Remove(HttpUtility.UrlEncode(keyName));
                    }
                    HttpContext.Current.Response.Cookies.Add(cookie);
                }
            }
        }
    }
}